package com.tenton.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenton.MyException.DataExist;
import com.tenton.MyException.DataNotExist;
import com.tenton.mapper.EmployeesMapper;
import com.tenton.pojo.Employees;
import com.tenton.service.EmployeesService;
import com.tenton.utils.Constants;
import com.tenton.utils.IntegerUtils;
import com.tenton.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@Service
public class EmployeesServiceImpl implements EmployeesService {
    @Autowired
    EmployeesMapper employeesMapper;

    /**
     * 登录验证
     *
     * @param empName
     * @param pwd
     * @return
     */
    @Override
    public boolean getLogin(String empName, String pwd, HttpSession session) {
        if (StringUtils.isEmpty(empName) && StringUtils.isEmpty(pwd)) {
            throw new NullPointerException("用户名或密码为空");
        } else {
            //查询用户信息
            Employees employee = employeesMapper.getLogin(empName, pwd);
            //判断登录的用户是否存在
            if (employee != null) {
                session.setAttribute("userId", employee.getId());
                session.setAttribute("name", empName);
                session.setAttribute(Constants.EMPLOYEE_SESSION, employee);
                return true;
            }
        }
        return false;
    }

    /**
     * 修改员工信息
     *
     * @param employees
     * @param session
     * @return
     */
    @Override
    public void update(Employees employees, HttpSession session) throws DataNotExist {
        if (employees == null){
            throw new DataNotExist("员工信息为空");
        }else {
            String name = (String) session.getAttribute("name");
            employees.setUpdateName(name);
            Date date = new Date();
            employees.setUpdateTime(date);
            employeesMapper.updateByPrimaryKey(employees);
        }
    }

    /**
     * 查询员工信息
     *
     * @param id
     * @return
     */
    @Override
    public Employees getEmployeeByPrimaryKey(Integer id) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("员工Id为空");
        } else {
            return employeesMapper.selectByPrimaryKey(id);
        }
    }

    /**
     * 查询员工信息
     *
     * @param id
     * @param model
     */
    @Override
    public void getEmployeesById(Integer id, Model model) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("员工Id为空");
        } else {
            model.addAttribute("employee", employeesMapper.selectByPrimaryKey(id));
        }
    }

    /**
     * 删除员工信息
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Integer id) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("员工Id为空");
        } else {
            return employeesMapper.deleteByPrimaryKey(id);
        }
    }

    /**
     * 添加员工
     *
     * @param employees
     * @param session
     * @return
     */
    @Override
    public void insert(Employees employees,HttpSession session) throws DataExist, DataNotExist {
        if (employees == null){
            throw new DataNotExist("员工信息为空");
        }else {
            if (employeesMapper.selectByIdCard(employees.getIdCard()) == null) {
                String name = (String) session.getAttribute("name");
                employees.setCreateName(name);
                Date date = new Date();
                employees.setCreateTime(date);
                employeesMapper.insert(employees);
            } else {
                throw new DataExist("此员工信息已存在");
            }
        }
    }

    /**
     * 获取所有员工信息
     *
     * @param model
     * @param pn
     * @return
     */
    @Override
    public void getAll(ModelMap model, Integer pn) {
        PageHelper.startPage(pn, Constants.PAGE_SIZE);
        List<Employees> employees = employeesMapper.selectAll();
        PageInfo<Employees> pageInfo = new PageInfo<Employees>(employees);
        model.addAttribute("pageInfo", pageInfo);
    }

    /**
     * 员工姓名模糊查询
     *
     * @param name
     * @param pn
     * @param model
     * @return
     */
    @Override
    public void getByParams(String name, Integer pn, ModelMap model) {
        PageHelper.startPage(pn, Constants.PAGE_SIZE);
        List<Employees> employees = employeesMapper.getByParams(name);
        PageInfo<Employees> pageInfo = new PageInfo<Employees>(employees);
        model.addAttribute("pageInfo", pageInfo);
    }
}
